﻿using System;
using System.Collections.Generic;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace KnowledgeKiosk.Converters
{
    public class DegreeToDirectionConverter : IValueConverter
    {
        public const double MAX_DEGREES = 360.0;

        public static List<String> WIND_DIRECTIONS = new List<string>
        {
            "N", "NNO", "NO", "ONO", 
            "O", "OZO", "ZO", "ZZO", 
            "Z", "ZZW", "ZW", "WZW", 
            "W", "WNW", "NW", "NNW"
        };

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            double degrees;
            if (double.TryParse(value.ToString(), out degrees))
            {
                double radianSlice = MAX_DEGREES / WIND_DIRECTIONS.Count;
                return WIND_DIRECTIONS[(int)((degrees % MAX_DEGREES) / radianSlice)];
            }
            else
            {
                return string.Empty;
            }
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            var windDirection = value.ToString();
            if (!string.IsNullOrEmpty(windDirection) && WIND_DIRECTIONS.IndexOf(windDirection) != -1)
            {
                double radianSlice = MAX_DEGREES / WIND_DIRECTIONS.Count;
                return WIND_DIRECTIONS.IndexOf(windDirection) * radianSlice;
            }
            else
            {
                return 0.0;
            }
        }
    }
}
